package hot100;

import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

public class GetIntersectionNode {
    @Test
    public void test() {
        List<Integer> collect = Arrays.stream(new int[]{1, 2, 3}).boxed().collect(Collectors.toList());
        collect.stream().map(x->x = x + 1);
        System.out.println(collect);
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        //定义Set
        Set<ListNode> exist = new HashSet<>();
        //1.遍历链表A，并存入A链表中的每个节点
        ListNode p = headA;
        while (p != null) {
            exist.add(p);
            p = p.next;
        }
        //2.遍历链表B，看节点是否在exist中存在，如果存在，则返回
        p = headB;
        while (p != null) {
            if (exist.contains(p)) return p;
            p = p.next;
        }
        //到最后都没找到，返回null
        return null;

    }
}
